/*
 * Created by SharpDevelop.
 * User: adriano
 * Date: 9/1/2008
 * Time: 0:44
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;

namespace Monorf.Net
{
    /// <summary>
    /// Description of Link.
    /// </summary>
    public class CommunicationLink<CommunicationData> where CommunicationData: new()
    {
        private Queue<CommunicationData> m_communicationData = null;
        private static Object m_lock = new Object();

        public event EventHandler<EventArgs> ReceivedData;


        public CommunicationLink()
        {
            m_communicationData = new Queue<CommunicationData>(30);
        }



        /// <summary>
        /// lock object
        /// </summary>
        public Object Lock
        {
            get { return m_lock; }
        }

        protected void OnReceivedData()
        {
            if (ReceivedData != null)
                ReceivedData(this, EventArgs.Empty);
        }

        #region methods

        /// <summary>
        /// receive data from input buffer 
        /// </summary>
        /// <returns></returns>
        public CommunicationData ReceiveData()
        {
            lock (m_communicationData)
            {
                if(m_communicationData.Count > 0)
                    return m_communicationData.Dequeue();
            }

            return default(CommunicationData);
        }

        /// <summary>
        /// post data to input buffer
        /// </summary>
        /// <param name="data"></param>
        public void PostData(CommunicationData data)
        {
            lock (m_communicationData)
            {
                m_communicationData.Enqueue(data);
            }

            OnReceivedData();
        }

        #endregion
    }
}
